<template>
  <div class="employees-container">
    <div class="app-container">
      <PageTools>
        <template #left>
          <i class="el-icon-info" />
          <span>共16条记录</span>
        </template>
        <template #right>
          <el-button type="danger" size="small" @click="handleDownload">普通excel导出</el-button>
          <el-button type="info" size="small">复杂标头ecxel导出</el-button>
          <el-button type="primary" size="small" @click="$router.push('/import')">导入excel</el-button>
          <el-button type="primary" size="small" @click="showClose">新增员工</el-button>
        </template>
      </PageTools>
      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="头像" prop="staffPhoto" />
          <el-table-column label="手机号" prop="mobile" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template #default="scope">
              <!-- 数组里的东西相当于key -->
              {{ heirMap[+scope.row.formOfEmployment] || '未知' }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间">
            <template #default="scope">
              {{ formMatedate(scope.row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <el-button type="text" size="small" @click="toDetail(scope.row.id)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delFn(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="center" align="middle" style="height:60px;">
          <el-pagination
            :page-size="params.size"
            :total="total"
            :current-page="params.page"
            layout="total, prev, pager, next"
            hide-on-single-page
            @current-change="currentChangeFn"
          />
        </el-row>
      </el-card>
    </div>
    <AddDialog :dialog-visible="dialogVisible" @close="dialogClose" @add="fetchEmployeeList" />
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api'
import AddDialog from './component/AddDialog.vue'
import dayjs from 'dayjs'
export default {
  components: {
    AddDialog
  },
  data() {
    return {
      params: {
        page: 1,
        size: 10
      },
      list: [],
      total: 0,
      heirMap: {
        1: '正式',
        2: '非正式'
      },
      dialogVisible: false
    }
  },
  created() {
    this.fetchEmployeeList()
  },
  methods: {
    toDetail(id) {
      this.$router.push({
        path: '/employeeDetail',
        query: { id }
      })
    },
    // 导出功能
    handleDownload() {
    // 动态引入 @/vendor/Export2Excel 包
      import('@/vendor/Export2Excel').then(excel => {
      // excel：是 @/vendor/Export2Excel 包中的 excel 导出模块
        // export_json_to_excel：将 json 数据转换为 excel
        excel.export_json_to_excel({
        // 表头 header 必填，是一个数组
          header: ['姓名', '年龄'],
          // 表格 body 具体数据，是一个二维数组
          data: [
            ['张三', 18],
            ['李四', 20]
          ],
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    // 删除员工
    delFn(val) {
      this.$confirm('确定删除么', '删除成功', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployee(val.id)
        this.$message.success('删除成功')
        if (this.list.length === 1 && this.params.page !== 1) {
          this.params.page--
        }
        this.fetchEmployeeList()
      })
    },
    // 弹出弹窗
    showClose() {
      this.dialogVisible = true
    },
    // 点击确定子传父关闭弹窗
    dialogClose() {
      this.dialogVisible = false
    },
    // 不同页面渲染不同的数据
    currentChangeFn(val) {
      this.params.page = val
      this.fetchEmployeeList()
    },
    // 渲染列表数据
    async fetchEmployeeList() {
      // 使用结构赋值的时候需要确认好结构的字段和对象中的字段是否一致
      // 使用了多层结构 （实际开发中不推荐）
      const { data: { rows, total }} = await getEmployeeList(this.params)
      this.list = rows
      this.total = total
    },
    formMatedate(value) {
      return dayjs(value).format('YYYY-MM-DD')
    }
  }
}

</script>
<style lang='less' scoped>
</style>
